<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="onPreview"
      :class="show? {hasImg: fileList.length>0} : {}"

      :on-remove="onRemove"
      :on-change="onChange"
      :http-request="httpRequest"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <el-progress v-if="isShow" :text-inside="true" :stroke-width="26" :percentage="percent" style="width: 146px;" />
    <!-- 弹窗 -->

    <el-dialog
      title="预览"
      :visible="showDialog"
      @close="showDialog=false"
    >
      <el-row
        type="flex"
        justify="center"
      >
        <img
          :src="previewImg"
          alt=""
        >
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShow: false,
      percent: 0,
      fileList: [],
      previewImg: '',
      showDialog: false
    }
  },
  methods: {

    onPreview(file) {
      this.previewImg = file.url
      this.showDialog = true
    },
    onRemove(file, newFileList) {
    //   console.log(file)
    //   console.log(newFileList)
      this.fileList = newFileList
    },
    onChange(file, newFileList) {
    //   console.log(63, file)
    //   console.log(64, newFileList)
      this.fileList = newFileList
    },
    beforeUpload(file) {
      const typeList = ['image/jpeg', 'image/png', 'image/gif']
      if (!typeList.includes(file.type)) {
        this.$message.error('目前只支持jpg/png/gif图片')
        return false
      }
      const maxSize = 1024 * 1024 * 10
      if (file.size > maxSize) {
        this.$message.error('图片大小不能超过1M')
        return false
      }
    },
    httpRequest(v) {
      // console.log('正式上传')

      this.isShow = true
      var cos = new COS({
        SecretId: 'AKID74kAKDpBG4apoVv4wXsxd2A1bvykFUS9',
        SecretKey: 'RbU5PSFl0xe4D5h25rMcaHhqiJejX4KI'
      })
      cos.putObject({
        Bucket: 'test-1309641525', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: v.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        StorageClass: 'STANDARD',
        Body: v.file, // 上传文件对象
        onProgress: (progressData) => {
          // console.log(JSON.stringify(progressData))

          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        // console.log(err || data)
        setTimeout(() => {
          this.isShow = false
        }, 1000)

        if (!err) {
          //   this.fileList.forEach(v => {
          //     v.status = 'success'
          //   })
          this.fileList[this.fileList.length - 1].url = 'http://' + data.Location
          this.fileList[this.fileList.length - 1].status = 'success'
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.hasImg {
    ::v-deep .el-upload--picture-card {
        display: none;
    }
}
::v-deep img {
  object-fit:cover;
}

</style>
